// pages/index/qclass/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    visible:false,
    code:'',
    isFa : '你的作业' ,
    identity: 0 ,
    classid: [] , 
    classList :[] 
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('userRank') ==  0 ) {
      this.setData({
        isFa : '你的作业',
        identity : 0
      })
      this.getStudentHomeWorkList();
    }else{
      this.setData({
        isFa : '发布的作业' ,
        identity: 1 
      })
      this.getTeacherHomeWorkList();
    }
  },
  getTeacherHomeWorkList(){
    wx.request({
      url: 'http://localhost:9090/api/homeWorkTeacher/list',
      data:{
        id :wx.getStorageSync('userInfo').user.id
      },
      method:'get',
      success: res=>{
        console.log(res,'11111');
        console.log(res.data,'222');
        console.log(res.data.data,'33');
        
        this.setData({
          list: res.data.data
        })
      }
    })
  },
  getStudentHomeWorkList(){
    console.log('当前用户：', wx.getStorageSync('userInfo'));
    wx.request({
      url: 'http://localhost:9090/api/homeWorkStudent/list',
      data:{
        id :wx.getStorageSync('userInfo').user.id
      },
      method:'get',
      success:res=>{
        this.setData({
          classList: res.data.data || []
        })
      }
    })
  },
  getList(){
    wx.request({
      url: 'http://localhost:9090/api/homeWorkTeacher/list',
      data:{
        id :wx.getStorageSync('userInfo').user.id
      },
      method:'get',
      success:res=>{
          this.setData({
            list:res.data
          })
      }
    })
  },
  //根据学生id  获取班级信息
  getClassByStudentId() { 
    wx.request({
      url: 'http://localhost:9090/api/joinClass/getStudentJoinClass',
      data:{
        userId:wx.getStorageSync('userInfo').user.id
      },
      method:'get',
      success:res=>{
        this.setData({
          classid :  res.data
        })
      }
    })
  },
  //根据班级获取 作业信息
  getClassHomeWorkList(){
    this.getClassByStudentId()
    setTimeout(() => {
      for(let i =  0 ; i < this.data.classid.data.length; i ++ ) { 
        wx.request({
          url: 'http://localhost:9090/api/homeWorkTeacher/getClassHomeWork',
          data:{
            id : this.data.classid.data[i].id,
            class :  this.data.classid.data[i].name
          },
          method:'get',
          success:res=>{
            this.setData({
              classList:res.data
            })
          }
        })
      }
    }, 100);
  },
  // 加入班级
  joinClass(){
    this.setData({
      code:'',
      visible:true
    })
  },
  cancelClass(){
    this.setData({
      
      visible:false
    })
  },
  confirmClass(){
    if(this.data.code == ''){
      wx.showToast({
        title: '请填写邀请码！',
        icon:'none'
      })
      return
    }
    wx.request({
      url: 'http://localhost:9090/api/joinClass/joinClassInsert',
      data:{
        key:this.data.code,
        userId:wx.getStorageSync('userInfo').user.id
      },
      method:'get',
      success:res=>{
        if(res.data.code == 200){
          wx.showToast({
            title: '加入成功！',
            icon:'none'
          })
        } else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
    this.setData({
      visible:false
    })
  },
  codeInput(e){
    this.setData({
      code:e.detail.value
    })
  },
  /**
   * uphomework    student
   */
  upHomeWork(e){
    wx.navigateTo({
      url: './upHomeWork/upHomeWork?id=' + e.currentTarget.dataset.id,
    })
  },
  // /**
  //  * getDetails teacher 
  //  */
  // getDetails(e){
  //   wx.navigateTo({
  //     url: './detail/detail?id=' + e.currentTarget.dataset.id,
  //   })
  // },
  getDetails(e) {
  wx.navigateTo({
      url: './studentHomeWorkDone/studentHomeWorkDone?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.data.identity === 0){
      this.getStudentHomeWorkList();
    }else{
      this.getTeacherHomeWorkList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})